;ledbpgp2s.sharper_collapse.du
;avance-version (18/10/22)
;written for TopSpin 4 and NEO console, for avIII+ the dwl_clk_on/off commands are different, see the acquisition part
;
;2D sequence for diffusion measurement using stimulated 
;   echo and LED based on Bruker's ledbpgp2s 
;using 2 spoil gradients and a compensating gradient
;collapsing all or some signals (selected by a band selective perfect echo) into a singlet
;optional solvent presaturation using pulsed presat or changing frequencies
;during relaxation and diffusion delays
;optional 13C decoupling
;
;G. Peat, P.J. Boaler, C.L. Dickson, G.C. Lloyd-Jones & D. Uhrin, Nat. Commun., 2023 
;
;diffusion part based on 
;D. Wu, A. Chen & C.S. Johnson Jr.,J. Magn. Reson. A 115, 260-264 (1995) 
;Pelta, MD, Morris, GA, Stchedroff, MJ and Hammond, SJ. Magn. Reson. Chem. 40:S147-S152 (2002)
;
;solvent suppression based on:
;Kew, W., Bell, N.G.A. Goodall, I., Uhrin, D., Magn. Reson. Chem. 55, 785-796, (2017) 
;For mutli-resonance suppression use Multi-reson-suppress.xlsx and PresatOptimise.jl to optimise p23 and o1
;
;optional band selective excitation based on perfect echo
;J. A. Aguilar, M. Nilsson, G. Bodenhausen and G. A. Morris, Chem. Commun., 2012, 48, 811
;
;for SHARRPER papers see: 
;Jones, A.B., Lloyd-Jones, G.C., Uhrin D, Anal. Chem. 89 10013-10021 (2017) 
;Dickson, C.L., Peat, G. Rossetto, M., Halse, M.E. and Uhrin, D. Chem. Commun., 58, 5534-5537 (2022)
;Davy, M., Dickson, C.L., Wei, R., Uhrin, D., Butts, C.P. Analyst, 147, 1702-1708 (2022)
;Silva-Terra, A. I., Rossetto, M., Dickson, C. L., Peat, G., Uhrin D., Halse, M. E., ACS Meas Sci Au. 3, 73-81 (2022).


; 
;$CLASS=HighRes
;$DIM=2D
;$TYPE=
;$SUBTYPE=
;$COMMENT=

#include <Avance.incl>
#include <Grad.incl>
#include <Delay.incl>
#include <De.incl>

define list<gradient> diff=<Difframp>

"p2=p1*2"

# ifdef P90
"p6=p5"
# else
"p6=p5*2"
# endif 

"d11=30m"
"d12=20u"

"d62=dw*l2"

# ifdef PURGE
"d13=4u"
"d63=(d62/2)-cnst13*dw"
"de=cnst13*dw-p1*2/PI"
# else
"d13=de"
"d63=d62/2"
"d14=d16+p1*2/PI-de"
# endif

"l0=(aq/d62-0.5)/2"
"l31=l0+4"

# ifdef PULSED_PRESAT
"FACTOR1=(d1/(p23))+ 0.5"
"l6=FACTOR1"
# endif

# ifdef PULSED_PRESAT1
"FACTOR2=(d20/(p24))+ 0.5"
"l7=FACTOR2"
"d22=l7*p24"
# endif


# ifdef PRESAT1

# ifdef PULSED_PRESAT1
"DELTA1=d22-p1*2-p2-p30*2-d16*4-p19*2-2*d12"
# endif

# ifdef PRESAT_JUMP1
"DELTA1=d20-p1*2-p2-p30*2-d16*4-p19*2-d12*4-4u"
# endif

# else
"DELTA1=d20-p1*2-p2-p30*2-d16*4-p19*2"
# endif

"DELTA2=d21-p19-d16-4u"

"acqt0=0"
baseopt_echo

dwellmode explicit


1 ze

# ifdef C13_DEC
  d12 pl12:f2
# endif

2 d11
  50u BLKGRAD

# ifdef PRESAT

# ifdef C13_DEC
  d12 cpds2:f2
# endif

# ifdef PULSED_PRESAT
  d12 pl0:f1
3 (p23:sp23 ph29):f1
  2u
  lo to 3 times l6
  d12 pl1:f1
# endif

# ifdef PRESAT_JUMP
  d12  fq=cnst23(bf):f1 ;solvent offset in Hz
  d12 pl9:f1
  d1 cw:f1 ph29
  4u do:f1
  d12 pl1:f1
  d12  fq=cnst24(bf):f1 ; real offset o1 in Hz
#endif

# ifdef C13_DEC
  d12 do:f2
# endif


# else
  d1 pl1:f1
#endif

  50u UNBLKGRAD

# ifdef PURGE1
  p19:gp7*-1
# else
  p19:gp4*-1
# endif
  
  d16
  p1 ph1
  p30:gp6*diff
  d16
  p2 ph1
  p30:gp6*-1*diff
  d16
  p1 ph2
  p19:gp7
  d16

# ifdef PRESAT1

# ifdef C13_DEC
  d12 cpds2:f2
# endif

# ifdef PULSED_PRESAT1
  d12 pl0:f1
4 (p24:sp24 ph29):f1
  2u
  lo to 4 times l7
  d12 pl1:f1
# endif

# ifdef PRESAT_JUMP1
  d12  fq=cnst23(bf):f1 ;solvent offset in Hz
  d12 pl9:f1
  DELTA1 cw:f1 ph29
  4u do:f1
  d12 pl1:f1
  d12  fq=cnst24(bf):f1 ;o1 offset in Hz
# endif

# ifdef C13_DEC
  d12 do:f2
# endif

# else
  DELTA1 pl1:f1
# endif


  p19:gp8*-1
  d16
  p1 ph3
  p30:gp6*diff
  d16
  p2 ph1
  p30:gp6*-1*diff
  d16
  p1 ph4
  p19:gp8
  d16
  DELTA2
  4u
 
  (p1 ph5)

# ifdef BSPE
  d13
  p19:gp1
  d16 pl0:f1

  4u
  (p11:sp1 ph6):f1
  4u

  p19:gp1
  d16
  d13 pl1:f1

  (p1 ph7)

  d13
  p19:gp2
  d16 pl0:f1

  4u
  (p11:sp1 ph6):f1
  4u

  p19:gp2

# ifdef PURGE
  d16 pl1:f1
  d13

# ifdef PURGE1
  p1 ph8
  4u
  p19:gp3
  d16
  p1 ph9
# endif

# ifdef PURGE2
  p1 ph8
  4u
  10u gron0
  (p32:sp29 ph1):f1
  20u groff
  d16
  p16:gp3
  d16 pl1:f1
  p1 ph9
# endif

# else
  d14
# endif

#endif

        ACQ_START(ph30,ph31)
        0.05u START_NEXT_SCAN  ;Topspin3: leave out
        0.1u REC_UNBLK
  	0.05u DWELL_RELEASE	;Topspin3: replace with DWL_CLK_ON
        d63 pl5:f1
        0.05u DWELL_HOLD  ;Topspin3: replace with DWL_CLK_OFF
        0.1u REC_BLK

5       d10 
        (p6 ph10):f1
        d10

        0.1u REC_UNBLK
        0.05u DWELL_RELEASE  ;Topspin3: replace with DWL_CLK_ON
        d62
        0.05u DWELL_HOLD  ;Topspin3: replace with DWL_CLK_OFF
        0.1u REC_BLK

        d10
        (p6 ph11):f1
        d10

        0.1u REC_UNBLK
        0.05u DWELL_RELEASE  ;Topspin3: replace with DWL_CLK_ON
        d62
        0.05u DWELL_HOLD  ;Topspin3: replace with DWL_CLK_OFF
        0.1u REC_BLK

        lo to 5 times l31
        d63
        

        rcyc=2

        d11 mc #0 to 2 F1QF(igrad diff)
        4u BLKGRAD
exit


ph1= 0
ph2= 0 0 2 2
ph3= 0 0 0 0 2 2 2 2  
ph4= 0 2 0 2 2 0 2 0  
ph5= 0 0 0 0 2 2 2 2 
ph6= 1
ph7= 1
ph8= 2
ph9= 0
ph10= {1}*8 {3}*8
ph11= {1}*8 {3}*8
ph29=0
ph30=(360) 0 ;set phcor30 to direct signal into one channel 
ph31=0 2 2 0 2 0 0 2 

;for presat during relaxation delay set
;zgoptns -DPRESAT -DPULSED_PRESAT or zgoptns -DPRESAT -DPRESAT_JUMP

;for presat during diffusion delay set
;zgoptns -DPRESAT1 -DPULSED_PRESAT1 or zgoptns -DPRESAT1 -DPRESAT_JUMP1

;for zgoptns -DPRESAT_JUMP or -DPRESAT_JUMP1 set
;cnst23: signal to be suppressed [Hz]
;cnst24: o1 [Hz]

;for zgoptns -DPULSED_PRESAT set 
;p23 : ~50 ms low power rectangular pulse
;if off-resonance, calculate its exact length as desctiben in the Supplementary Information 

;if only one signal needs to be suppressed:
;SPNAM23 : Squa100.1000
;SPOFFS23 : resonance frequency of the suppressed signal minus o1 [Hz]
;spdb23 : use minimum power start with 60dB, asses the result

;if multiple signals need to be suppressed use Multi-reson-suppress.xlsx and PresatOptimise.jl to optimise p23 and o1
;create a phase-ramped shape using the calulated pulse length and the distances from o1
;prepare a rectangular shape and place it into the user library, e.g. Squa100.1000 (100us, 1000 points)
;on a topspin command line type:
;st manipulate /opt/topspin4.1/exp/stan/nmr/lists/wave/user/Squa100.1000 offs e 68936 3 565.73 -580.27 -754.27
;this will OVERWRITE the Squa100.1000 shape in /opt/topspin4.1/exp/stan/nmr/lists/wave/user/
;creating a 68936 us shape that will irradiate at 565.73, -580.27 and -754.27Hz away from optimised o1
;Note: for n frequencies replace 3 with n
;spdb23 : use minimum power, start with 60dB, asses the result. For every addtional frequency adjust by subtracting 6dB.

;for zgoptns -DPULSED_PRESAT1 repeat the process to set p24, SPNAM24, SPOFFS24 and spdb24, use shorter p24 (~ 10-20ms)

;for selective 13C decoupling zgoptns -DC13_DEC
;for band selective perfect echo set zgoptns -DBSPE
;for z-filter set zgoptns -DPURGE -DPURGE1 or zgoptns -DPURGE -DPURGE2
;for 90 deg spin-echo pulses set zgoptns -DP90

;if zgoptns -DC13_DEC:
;o2p : 13C chemical shift of the suppressed solvent signal
;pldb12 : power level for 13C decoupling
;at least 12dB weaker than regular decoupler power
;pcpd2 : around 200us for selective decoupling
;cpdprg2 : xy32 or garp4

;DIGMOD = baseopt
;p1  : f1 channel -  90 degree high power pulse
;p2  : f1 channel - 180 degree high power pulse
;p5 : f1 channel - 90 degree lower power pulse for spin-echoes
;p6 : f1 channel - 180 degree lower power pulse for spin-echoes
;pl1 : f1 channel - power level for pulse (default)
;pldb1 : f1 channel -  high power
;pl5 : f1 channel - power level for spin-echo pulses IMPORTANT! (see SI of the Peat et al paper)
;pldb5 : f1 channel -  lower power

;p19: gradient pulse 2 (spoil gradient)
;p30: gradient pulse (little DELTA * 0.5)

;d1  : relaxation delay; 1-5 * T1
;d10: ringdown time - microseconds to hudreads of microseconds
;d11: delay for disk I/O [30 msec]
;d12: delay for power switching                         [20 usec]
;d16: delay for homospoil/gradient recovery             [200 usec]
;d20: diffusion time (big DELTA)
;d21: eddy current delay (Te)			[5 ms]
;NS: 8 * n
;DS: 4 * m
;td1: number of experiments
;FnMODE: QF
;        use xf2 and DOSY processing
;l0 : number of blocks during acquisition time adjusted to get d62 as required
;l2 : number of points in full chunk [2-16] depending on dw = 1/2*SWH (increase SWH to decrease dw)
;d62: dw*l2 acquisition block between decoupling pulses [< 0.5msec]
;d63: = d62/2, length of initial half chunk if PURGE not used
;d63: d62/2-cnst13*dw if PURGE used
;cnst13: typically 1,increse SWH if negative
;d10: delay around spin-echo pulses [ 5-20 us]

;for zgoptns -DBSPE
;p11: f1 channel - 180 degree shaped pulse (Reburp.1000)   [0.5-3 msec]
;pl9 : f1 channel - power level for presaturation [~55-69dB]
;sp1:f1 channel - 180 degree shaped pulse (Reburp.1000)   [0.5-3 msec]
;spnam1: Reburp.1000
;spdb1: power level for sp1

;for zgoptns -DPURGE2 set
;sp29: f1 channel - shaped pulse (adiabatic)
;p32: f1 channel - 180 degree shaped pulse (adiabatic)    [20 msec]
;     smoothed chirp (sweep width, 20% smoothing, 10000 points)

; Instructions for setting receiver phase, ph30
;   1) Record spectrum with PH30 = (360) 0 and phcor30=0
;   2) Phase the spectrum using PHC0 only
;   3) If PHCO > 0 set phcor30 = 360-PHC0, if PHC0 < 0 set phcor = -PHC0
;   4) Set PHC0=0 PHC1=0
;   5) If the phase changes, rephase, do not store PHC0, set phcor30 = phcor30 - PHC0

;for z-only gradients:
;gpz0: ca. 11%
;gpz1: 7%
;gpz2: 5%
;gpz3: -12%
;gp4: gp7+2*gp1+2*gp2
;gpz6: 100%
;gpz7: -17.13% (spoil)
;gpz8: -13.17% (spoil)

;use gradient files:   
;gpnam1: SINE.100
;gpnam2: SINE.100
;gpnam3: SINE.100
;gpnam4: SINE.100
;gpnam6: SINE.100
;gpnam7: SINE.100
;gpnam8: SINE.100

;use AU-program dosy to calculate gradient ramp-file Difframp
